/*
 * Copyright (c) YouYuan 2018. https://gitee.com/you-yuan
 */

package com.yuan.crawler.proxy;

import java.util.Date;

/**
 * IP代理pojo对象
 * @author YouYuan
 * @version 1.0
 * @CreateDate 2018/5/24 16:23
 * @UpdateDate 2018/5/24 16:23
 */
public class IPProxy implements Comparable<IPProxy>{

    /**
     * ip地址
     */
    private String ip;
    /**
     * 端口
     */
    private int port;
    /**
     * 有效期
     */
    private Date expires;
    /**
     * 是否使用过
     */
    private boolean used = false;



    public String getIpAndPort() {
        return ip + ":" + port;
    }

    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public Date getExpires() {
        return expires;
    }

    public void setExpires(Date expires) {
        this.expires = expires;
    }

    public boolean isUsed() {
        return used;
    }

    public void setUsed(boolean used) {
        this.used = used;
    }

    /**
     * IP代理对象排序算法：
     * 使用过的代理优先排在前面
     * 若均未使用，则有效时间更长的排在前面
     * @param o
     * @return
     */
    @Override
    public int compareTo(IPProxy o) {
        if (this == o) {
            return 0;
        }
        if (o == null) {
            return 1;
        }
        if (this.used && !o.used) {
            return -1;
        }
        if (!this.used && o.used) {
            return 1;
        }
        Date expires = o.getExpires();
        if (expires == null) {
            return -1;
        }
        return -this.expires.compareTo(expires);
    }

    @Override
    public String toString() {
        return "IPProxy{" +
                "ip='" + ip + '\'' +
                ", port=" + port +
                ", expires=" + expires +
                ", used=" + used +
                '}';
    }
}
